<?php

namespace App\Repositories;

use App\Models\OpenArea;

class AreaRepository
{
    public function getAreas($filter, $tree = true)
    {
        $areas = OpenArea::query()
//            ->where($filter)
            ->when(!empty($filter['level']), function ($q) use ($filter) {
                if (is_array($filter['level'])) {
                    return $q->where("level", $filter['level'][0], $filter['level'][1]);
                }
            })
            ->orderBy("id", "asc")
            ->orderBy("sort", "desc")
            ->get(['id', "pid", 'title'])
            ->toArray();

        if ($tree)
            return list_to_tree($areas, "id", "pid");

        return $areas;
    }

    public function getTestAreas($filter, $tree = true)
    {
        $areas = OpenArea::query()
            ->where($filter)
            ->where("letter", "<>", "")
            ->where("pid", "!=", 0)
            // ->orderBy("id", "asc")
            ->orderBy("letter", "asc")
            ->get(['id', "pid", 'title', 'letter'])
            ->toArray();

        // dd($areas);

        return ass_column($areas, "letter", true);
    }
}
